﻿Imports System.Data
Partial Class WebForm_ManageRetailPrice
    Inherits System.Web.UI.Page
    Dim lManager As New LevelManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLDivision()
            Me.BindDDLSignature()
            Me.InitDataAndShow()
        End If
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.InitDataAndShow()
    End Sub

    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim sCoefficient As String
        Dim pValue As Decimal

        Dim sSignature As String
        Dim cuser As String = clsAuth.GetUserID()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        For i As Integer = 0 To Me.gv.Rows.Count - 1
            sCoefficient = TryCast(Me.gv.Rows(i).FindControl("txtCoefficient"), TextBox).Text
            If Decimal.TryParse(sCoefficient, pValue) Then
                sSignature = Me.gv.DataKeys(i).Item("Signature").ToString()

                sql.Length = 0
                sql.AppendFormat(" UPDATE RetailPriceCoefficient ")
                sql.AppendFormat(" SET Coefficient = {0} ", pValue)
                sql.AppendFormat(" ,UpdateBy ='{0}' ", cuser)
                sql.AppendFormat(" ,UPdateDate =GetDate() ")
                sql.AppendFormat(" WHERE 1=1 ")
                sql.AppendFormat(" AND Signature ='{0}'", sSignature)
                sql.AppendFormat(";INSERT RetailPriceCoefficient(Signature,Coefficient,CreateBy,CreateDate) ")
                sql.AppendFormat(" Select ")
                sql.AppendFormat("  '{0}' ", sSignature)
                sql.AppendFormat(" ,{0} ", pValue)
                sql.AppendFormat(" ,'{0}' ", cuser)
                sql.AppendFormat(" ,GetDate() ")
                sql.AppendFormat(" where not exists ")
                sql.AppendFormat(" ( ")
                sql.AppendFormat("  SELECT top 1 * FROM RetailPriceCoefficient ")
                sql.AppendFormat("  WHERE Signature ='{0}' ", sSignature)
                sql.AppendFormat(" ) ")
                'MyObj.WriteLog(sql.ToString())
                dat.AccData(sql.ToString)
            End If
        Next
        MyObj.ShowMessage(Me.GetLocalResourceObject("SaveDone"))
    End Sub

#Region "Gridview事件"
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.InitDataAndShow()
    End Sub

    Protected Sub gv_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.PageIndexChanged
        InitDataAndShow()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

        End If
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub

    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetSignatureNoNewCategories(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub
#End Region

#Region "方法"
    Public Sub InitDataAndShow()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim hmManager As New HierarchyManager

        'Search 條件
        sql.AppendFormat(" SELECT Distinct A.Division,C.Code as Signature,C.Description as SignatureText ")
        sql.AppendFormat(" ,B.Coefficient ")
        sql.AppendFormat(" FROM (Select distinct Division,Signature from ChannelView) A LEFT JOIN RetailPriceCoefficient B ")
        sql.AppendFormat(" ON A.[Signature] = Left(B.[Signature],2) ")
        sql.AppendFormat(" LEFT JOIN  ProductHierarchyOriginal C ")
        sql.AppendFormat(" ON A.Signature = C.CODE AND C.LevelName='Signature' ")
        sql.AppendFormat(" WHERE 1=1 ")
        If ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat(" AND A.Division='{0}' ", ddlDivision.SelectedValue)
        Else
            Dim dvManager As New DivisionManager
            sql.AppendFormat(" AND A.Division IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", dvManager.GetDivisions()))
        End If

        If ddlSignature.SelectedValue <> "" Then
            sql.AppendFormat(" AND A.Signature ='{0}' ", Left(ddlSignature.SelectedValue, 2))
        Else
            Dim stManager As New SignatureManager
            sql.AppendFormat(" AND A.Signature IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", stManager.GetSignatures(ddlDivision.SelectedValue)))
        End If

        sql.AppendFormat(" Order by A.Division, C.Description ")
        'MyObj.WriteLog(sql.ToString())
        Dim dt As DataTable = dat.GetData(sql.ToString())

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub
#End Region
End Class

